package com.alen.sun.controller;

import com.alen.sun.bean.*;
import com.alen.sun.dao.ElderlyMapper;
import com.alen.sun.service.ActivityService;
import com.alen.sun.service.ElderlyService;
import com.alen.sun.util.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@CrossOrigin
@RequestMapping("/elderlys")
@RestController
public class ElderlyController {
    @Autowired
    private ElderlyService elderlyService;
    @Autowired
    private ElderlyMapper elderlyMapper;


    /**
     * 老人登录
     * @param elderly
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody Elderly elderly){
        System.out.println("---------");
        log.info("{}",elderly);
        System.out.println("---------");
        Elderly elderly1 = elderlyService.login(elderly);
        if (elderly1!=null){
            //登录成功，生成令牌，下发令牌
            Map<String,Object> claims=new HashMap<>();
            claims.put("elderly",elderly1);
            String jwt=JwtUtils.generateJwt(claims);//jwt包含了当前登录的老人信息
            return Result.success(jwt);
        }else {
            //登录失败，返回错误信息
            return Result.error("用户名或密码错误");
        }
    }

    /**
     * 添加老人
     * @param elderly
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Elderly elderly){
        Boolean flag = elderlyService.add(elderly);
        if(flag) {
            //true--注册成功
            return Result.success();
        }else {
            //false--注册失败
            return Result.error("添加错误，请重试");
        }
    }


    /**
     * 通过护工Id获取老人列表
     * @param request
     * @return
     */
    @GetMapping("/getByEmp")
    public Result listElderlysForEmployees(HttpServletRequest request){
        Employee employee=(Employee)request.getAttribute("employee");
        log.info("getByEmp被使用");
        List<Elderly> elderlies=elderlyService.getElderlysForEmployee(employee.getEmployeeId());
        log.info("{}",elderlies);
        if(elderlies!=null){
            return Result.success(elderlies);
        }else {
            log.info("未查询到老人数据");
            return Result.error("暂无老人数据");
        }
    }

    /**
     * 通过老人Id获取老人
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable String id){
        Elderly elderly=elderlyService.getById(id);
        return Result.success(elderly);
    }

    /**
     * 根据ID删除老人
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(@RequestParam String id){
        Boolean flag=elderlyService.delete(id);
        if (flag){
            return Result.success();
        }else {
            return Result.error("删除失败");
        }
    }

    /**
     * 修改老人
     * @param elderly
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Elderly elderly){
        Boolean flag=elderlyService.update(elderly);
        if (flag){
            return Result.success();
        }else {
            return Result.error("抱歉，修改失败");
        }

    }

    /**
     * 老人名字查询
     * @param word
     * @return
     */
    @GetMapping("/search")
    public Result word(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "5") Integer pageSize,
                       @RequestParam String word){
        PageBean pageBean = elderlyService.page(page,pageSize,word);
        log.info("search被使用");
        return Result.success(pageBean);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/ids")
    @ResponseBody
    public Result deleteByIds(@RequestBody List<String> ids) {
        Boolean flag = elderlyService.deleteByIds(ids);
        if(flag){
            return Result.success();
        }else {
            return Result.error("批量删除失败");
        }

    }
//    /**
//     * 分配房间
//     * @param elderly
//     * @return
//     */
//    @PutMapping("/setRoomForElderly")
//    @ResponseBody
//    public Result setRoomForElderly(@RequestBody Elderly elderly){
//        Boolean flag = elderlyService.setRoomForElderly(elderly);
//        if(flag){
//            return Result.success();
//        }else {
//            return Result.error("分配房间失败");
//        }
//    }
//
//    /**
//     * 分页查询,未分配的老人信息
//     * @param requestParams
//     * @return
//     */
//    @PostMapping("/pageNoHaveEmployee")
//    public Result pageNoHaveEmployee(@RequestBody Map<String, Integer> requestParams){
//        Integer page = requestParams.getOrDefault("page", 0);
//        Integer pageSize = requestParams.getOrDefault("pageSize", 10);
//        PageBean pageBean = elderlyService.pageNoHaveEmployee(page, pageSize);
//        return Result.success(pageBean);
//    }
//
//    /**
//     * 分配护工给老人
//     * @param elderly
//     * @return
//     */
//    @PutMapping("/setEmployeeIdForElderly")
//    public Result setEmployeeIdForElderly(@RequestBody Elderly elderly){
//        Boolean flag = elderlyService.setEmployeeIdForElderly(elderly);
//        if(flag){
//            return Result.success();
//        }else {
//            return Result.error("分配护工给老人失败");
//        }
//    }

}
